﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using FlyHorse.LIMS.Model;
using FlyHorse.LIMS.WinApp.LanguageResource;

namespace FlyHorse.LIMS.WinApp.BaseManageForms.SamplingPlaceForms
{
    public partial class SamplingPlaceMainForm : DevExpress.XtraEditors.XtraForm
    {
        BLL.SamplingPlaceBll samplingPlaceBll = new BLL.SamplingPlaceBll();

        public SamplingPlaceMainForm()
        {
            InitializeComponent();
        }

        private void SamplingPlaceMainForm_Load(object sender, EventArgs e)
        {
            this.gridView1.FocusRectStyle = DevExpress.XtraGrid.Views.Grid.DrawFocusRectStyle.RowFocus;
            this.gridView1.OptionsBehavior.Editable = false;
            this.gridView1.OptionsSelection.EnableAppearanceFocusedCell = false;
            InitGrid();
        }

        private void InitGrid()
        {
            var models = samplingPlaceBll.LoadEntitiesAsNoTracking(m => true).OrderByDescending(m => m.SamplingPlaceID).ToList();
            this.gridControl1.DataSource = models;
        }

        private void ButtonItem_add_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            SamplingPlaceDetailForm samplingPlaceDetailForm = new SamplingPlaceDetailForm(InitGrid);
            samplingPlaceDetailForm.Show();

        }

        private void ButtonItem_Delete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (this.gridView1.GetFocusedRowCellValue("SamplingPlaceID") == null)
            {
                XtraMessageBox.Show(ResourceOverall.SelectNull);
                return;
            }
              string id = Convert.ToString(this.gridView1.GetFocusedRowCellValue("SamplingPlaceID"));
                SamplingPlace place = this.samplingPlaceBll.LoadEntities(m => m.SamplingPlaceID == id).FirstOrDefault();
                if (place != null)
                {
                    int count = new BLL.ReportSemiBll().LoadEntities(m => m.SamplingPlaceID == id).Count();
                    if (count > 0)
                    {
                        XtraMessageBox.Show(ResourceOverall.DeleteExists);
                        return;
                    }
                    DialogResult result = XtraMessageBox.Show(ResourceOverall.ConfirmDelete, ResourceOverall.SystemWarningStr, MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                    if (result == DialogResult.OK)
                    {
                        bool res = samplingPlaceBll.DeleteSamplingPlaceAndStandard(place);
                        if (res)
                        {
                            InitGrid();
                            XtraMessageBox.Show(ResourceOverall.DeleteSucceed);
                        }
                    }
                }
            
        }

        private void ButtonItem_edit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

            if (this.gridView1.GetFocusedRowCellValue("SamplingPlaceID") == null)
            {
                XtraMessageBox.Show(ResourceOverall.SelectNull);
                return;
            }
            string id = Convert.ToString(this.gridView1.GetFocusedRowCellValue("SamplingPlaceID"));
            SamplingPlace place = samplingPlaceBll.LoadEntities(m => m.SamplingPlaceID == id).FirstOrDefault();
            if (place != null)
            {
                DialogResult result = XtraMessageBox.Show(ResourceOverall.ConfirmEdit, ResourceOverall.SystemWarningStr, MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                if (result == DialogResult.OK)
                {
                    SamplingPlaceDetailForm samplingPlaceDetailForm = new SamplingPlaceDetailForm(place, InitGrid);
                    samplingPlaceDetailForm.Show();
                }
            }
        }
    }
}